var vue=new Vue({
   el:'#main',
   data:{
       role:{
           id:'',
           name:''
       },
       privileges:[],
       oldprivileges:[],
       newprivileges:[]
   },
    methods:{
       selectOne:function () {
           axios({
               url:`/role/selectOne`,//   有/斜杆的是绝对路径，没有的是相对路径
               method:'get',
               params: {
                   Rid:this.role.id
               }
           }).then(response=>{
               //layer.msg("查询成功");
               this.role=response.data.obj;
               console.log(response.data.obj);
           }).catch(error=>{
               layer.msg(error.message)
           })
       },
        selectPrivileges:function () {
            axios({
                url:`/privilege/selectAll`,//   有/斜杆的是绝对路径，没有的是相对路径
                method:'get',
            }).then(response=>{
                //layer.msg("查询成功");
                this.privileges=response.data.obj;
                for (let i = 0; i < this.privileges.length; i++) {
                    Vue.set(this.privileges[i],"checked",false);
                }
            }).catch(error=>{
                layer.msg(error.message)
            })
        },
        selectPrivilegesByRid:function () {
            axios({
                url:`/privilege/selectPrivilegesByRid`,//   有/斜杆的是绝对路径，没有的是相对路径
                method:'get',
                params: {
                    Rid:this.role.id
                }
            }).then(response=>{
                //layer.msg("查询成功");
                this.oldprivileges=response.data.obj;
                this.newprivileges=response.data.obj;
                for (let i = 0; i < this.privileges.length; i++) {
                    for (let j = 0; j < this.oldprivileges.length; j++) {
                        if (this.privileges[i].privilegeId==this.oldprivileges[j].privilegeId){
                            this.privileges[i].checked=true;
                        }
                    }
                }
            }).catch(error=>{
                layer.msg(error.message)
            })
        },
        addPrivilege:function (privilege) {
            for (let i = 0; i < this.privileges.length; i++) {
                if (this.privileges[i].privilegeId==privilege.privilegeId){
                    this.privileges[i].checked=!this.privileges[i].checked;
                    if (this.privileges[i].checked){
                        this.newprivileges.push(this.privileges[i]);
                    }
                    else {
                        for (let j = 0; j < this.newprivileges.length; j++) {
                            if (this.newprivileges[j].privilegeId==this.privileges[i].privilegeId){
                                this.newprivileges.splice(j,1);
                            }
                        }
                    }
                }
            }
            console.log(this.newprivileges);
        },
        doUpdate:function () {
            if (this.newprivileges.length>0&&this.role.roleName!=''){
                axios({
                    url:'/role/doUpdate',
                    method:'put',
                    data:{
                        Rid:this.role.roleId,
                        name:this.role.roleName,
                        privileges:this.newprivileges
                    }
                }).then(response=>{
                    if (response.data.success){
                        parent.layer.message='success';
                        let index=parent.layer.getFrameIndex(window.name);
                        parent.layer.close(index);
                    }
                }).catch(error=>{
                    layer.msg(error.message);
                })
            }else if (this.newprivileges.length < 0) {
                layer.msg("请至少选择一个角色");
            }else if (this.role.roleName==''){
                layer.msg("名字不能为空");
            }
        },
    },
    created:function () {
       this.role.id=parent.layer.obj;
       this.selectOne();
       this.selectPrivileges();
       this.selectPrivilegesByRid();
    }
});